import java.applet.*;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

// creates and runs the tetris game
public class TetrisGame extends Applet implements Runnable, MouseListener, KeyListener{
	private Grid gr;
	private int length=1000;//approximate length of the screen
	private int width=1500;//approximate width of the screen
	Graphics g;
	private int UpCount;
	private int SideCount;
	private int score;
	private double rotation;
	

	public void init(){
		setSize(width,length);
		setBackground(Color.BLACK);
		gr=new Grid(10,25,Color.GRAY, (Graphics2D)getGraphics(),width,length, this);
		gr.insertPiece();
		Thread thread=new Thread(this);
		thread.start();
		addKeyListener(this);
		addMouseListener(this);
		score=0;
		UpCount=0;
		SideCount=0;
		rotation=0;
		
	}
	public double getRotation(){
		return rotation;
	}
	
	public int getScore(){
		return score;
	}
	public int getSideCount(){
		return SideCount;
	}
	public void paint(Graphics g){
		this.g=g;
		int transX=0;
		int transY=0;
		if(rotation==Math.PI/2){
			transY=length;
		}else if(rotation==Math.PI){
			transX=width;
			transY=length;
		}else if(rotation==3*Math.PI/4){
			transY=length;
		}
		g.translate(gr.getSize()*SideCount+transX,gr.getSize()*-1*UpCount+transY);
		((Graphics2D)g).rotate(rotation);
//		g.translate(0,gr.getSize()*-1*UpCount);
		gr.paint((Graphics2D)g);

	}

	
	public Grid getGr() {
		return gr;
	}
	public void setGr(Grid gr) {
		this.gr = gr;
	}
	public int getLength() {
		return length;
	}
	public void setLength(int length) {
		this.length = length;
	}
	public int getWidth() {
		return width;
	}
	public void setWidth(int width) {
		this.width = width;
	}
	public Graphics getG() {
		return g;
	}
	public void setG(Graphics g) {
		this.g = g;
	}
	public int getUpCount() {
		return UpCount;
	}
	public void setUpCount(int upCount) {
		UpCount = upCount;
	}
	public void setSideCount(int sideCount) {
		SideCount = sideCount;
	}
	public void setScore(int score) {
		this.score = score;
	}
	public void setRotation(double rotation) {
		this.rotation = rotation;
	}
	@Override
	public void run() {
		while(true){
		try {
			Thread.sleep(1000);
			//Thread.sleep(250);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		UpCount++;
//=======
//		if(gr.getReset()){
//			upCount=0;
//		}
//		upCount++;
//		gr.setReset();
//>>>>>>> .r8
		repaint();
		}
		
	}
	public void resetCount(){
		UpCount=0;
		rotation=0;
		SideCount=0;
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		System.out.println("Mouse cluicked");
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub
		System.out.println(e.getKeyCode());
		if(e.getKeyCode()==KeyEvent.VK_DOWN||e.getKeyCode()==KeyEvent.VK_KP_DOWN){
			System.out.println(UpCount);
			UpCount++;
			System.out.println(UpCount);
			repaint();
		}
		if(e.getKeyCode()==KeyEvent.VK_SPACE){
			System.out.println("space");
			rotation+=Math.PI/2;
			repaint();
		}
		if(e.getKeyCode()==KeyEvent.VK_LEFT||e.getKeyCode()==KeyEvent.VK_KP_LEFT){
			SideCount--;
			ArrayList<Piece> pieces=gr.getPieces();
			Piece p=pieces.get(pieces.size()-1);
				ArrayList<Block>blocks=p.getBlocks();
				for(Block b:blocks){
					b.moveX(gr.getSize());
				}
		}
		if(e.getKeyCode()==KeyEvent.VK_RIGHT||e.getKeyCode()==KeyEvent.VK_KP_RIGHT){
			SideCount++;
			ArrayList<Piece> pieces=gr.getPieces();
			for(int i=0;i<pieces.size();i++){
				Piece p=pieces.get(i);
				ArrayList<Block>blocks=p.getBlocks();
				for(Block b:blocks){
					b.moveX(gr.getSize()*-1);
				}
			} 
		}
	}


	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void keyTyped(KeyEvent e) {
		System.out.println(e.getKeyCode());
		if(e.getKeyCode()==KeyEvent.VK_DOWN||e.getKeyCode()==KeyEvent.VK_KP_DOWN){
			System.out.println(UpCount);
			UpCount++;
			System.out.println(UpCount);
			repaint();
		}
		
		
		
		
	}


}
